/*
 * PseudoType.java
 * 
 * Created on May 30, 2007, 8:00:49 AM
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import java.util.StringTokenizer;

/**
 *
 * @author tim
 */
public class PseudoType
{
     public static void convert(String line, int lineNumber, Assembled assembled)
     {
	  StringTokenizer tokenizer = new StringTokenizer(line, " ,");
	  int[] type = table(tokenizer.nextToken());
	  if(type[0] == 6)
	       rconvert(type, line, lineNumber, assembled);
	  else if(type[0] == 7)
	       iconvert(type, line, lineNumber, assembled);
	  else
	       assembled.addError("error at line number " + lineNumber + "\nunrecognized command " + line);
     }
     public static int[] table(String command)
     {
	  int[] type = new int[3];
	  type[0] = -1;
	  type[1] = -1;
	  
	  if(command.equals("div"))
	  {
	       type[0] = 6;
	       type[1] = 0;
	  }
	  else if(command.equals("mul"))
	  {
	       type[0] = 6;
	       type[1] = 1;
	  }
	  else if(command.equals("mult"))
	  {
	       type[0] = 6;
	       type[1] = 2;
	  }
	  else if(command.equals("divi"))
	  {
	       type[0] = 7;
	       type[1] = 0;
	  }
	  else if(command.equals("muli"))
	  {
	       type[0] = 7;
	       type[1] = 1;
	  }
	  else if(command.equals("multi"))
	  {
	       type[0] = 6;
	       type[1] = 2;
	  }
	  return type;
     }
     public static void rconvert(int[] type, String line, int lineNumber, Assembled assembled)
     {
	  StringTokenizer tokenizer = new StringTokenizer(line, " ,");
	  String garbage = tokenizer.nextToken();
	  int[] build;
	  
	  switch(type[1])
	  {
	  case 0: //div
	       build = new int[5];
	       build[0] = type[0];
	       build[1] = type[1];
	       build[2] = Memory.memory(tokenizer.nextToken());
	       build[3] = Memory.memory(tokenizer.nextToken());
	       build[4] = lineNumber;
	       assembled.addLine(build);
	       break;
	  case 1: //mul
	       build = new int[6];
	       build[0] = type[0];
	       build[1] = type[1];
	       build[4] = Memory.memory(tokenizer.nextToken());
	       build[2] = Memory.memory(tokenizer.nextToken());
	       build[3] = Memory.memory(tokenizer.nextToken());
	       build[5] = lineNumber;
	       assembled.addLine(build);
	       break;
	  case 2: //mult
	       build = new int[5];
	       build[0] = type[0];
	       build[1] = type[1];
	       build[2] = Memory.memory(tokenizer.nextToken());
	       build[3] = Memory.memory(tokenizer.nextToken());
	       build[4] = lineNumber;
	       assembled.addLine(build);
	       break;
	  }
     }
     public static void iconvert(int[] type, String line, int lineNumber, Assembled assembled)
     {
	  StringTokenizer tokenizer = new StringTokenizer(line, " ,");
	  String garbage = tokenizer.nextToken();
	  int[] build;
	  
	  switch(type[1])
	  {
	  case 0: //divi
	       build = new int[5];
	       build[0] = type[0];
	       build[1] = type[1];
	       build[2] = Memory.memory(tokenizer.nextToken());
	       build[3] = Integer.parseInt(tokenizer.nextToken());
	       build[4] = lineNumber;
	       assembled.addLine(build);
	       break;
	  case 1: //muli
	       build = new int[6];
	       build[0] = type[0];
	       build[1] = type[1];
	       build[3] = Memory.memory(tokenizer.nextToken());
	       build[2] = Memory.memory(tokenizer.nextToken());
	       build[4] = Integer.parseInt(tokenizer.nextToken());
	       build[5] = lineNumber;
	       assembled.addLine(build);
	       break;
	  case 2: //multi
	       build = new int[5];
	       build[0] = type[0];
	       build[1] = type[1];
	       build[2] = Memory.memory(tokenizer.nextToken());
	       build[3] = Integer.parseInt(tokenizer.nextToken());
	       build[4] = lineNumber;
	       assembled.addLine(build);
	       break;
	  }
     }
}
